import { Login } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    // 把数据存到cookie本地中
    token: getToken() || ''
  },
  mutations: {
    // vuex存token
    SetToken(state, token) {
      state.token = token
      // 存入cookies
      setToken(token)
    },
    // 清空token
    RemoveToken(state) {
      state.token = ''
      removeToken()
    }
  },
  actions: {
    // token
    async SetTokenFn(store, data) {
      const res = await Login(data)
      store.commit('SetToken', res.token)
      return res
    }
  }
}
